/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    gongwenkai
 * 创建时间:    2023-09-26
 */
package org.jeecg.modules.flow.callback.outbound.task;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.third.entity.EmsOrDmsDeliveryDto;
import org.jeecg.modules.business.third.service.IEmsOrDmsDeliveryOperateService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 叠片叫料 通知叫料 回调Bean
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-26
 */
@RequiredArgsConstructor
@Component
public class OutBoundTaskDPJLNoticeBean<T> extends BaseCustomBeanRun<T> {

    private final IEmsOrDmsDeliveryOperateService emsOrDmsDeliveryOperateService;

    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;

    /**
     * 业务逻辑 叠片叫料 通知叫料
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String taskId = baseFlowDto.getInfoId();
        // 主动调用EMS发送叫料板请求
        EmsOrDmsDeliveryDto emsOrDmsDeliveryDto = new EmsOrDmsDeliveryDto();
        emsOrDmsDeliveryDto.setTaskId(taskId);
        boolean successFlag = emsOrDmsDeliveryOperateService.outbound(emsOrDmsDeliveryDto);
//        baseFlowDto.setCurrentFailed(successFlag);

        wmsOutReceiptTaskService.update(new LambdaUpdateWrapper<WmsOutReceiptTask>()
                .set(WmsOutReceiptTask::getDocumentStatus, DocumentStatusEnum.PROCESS.getCode())
                .eq(WmsOutReceiptTask::getId, taskId));
        // 当前前端调用业务需要你返回的
        baseFlowDto.setResult(Result.OK());
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(taskId);
    }
}
